<?php
/**
 * 我的代金券
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */



defined('ByShopWWI') or exit('Access Invalid!');

class member_pointsControl extends mobileMemberControl {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 签到列表
     */
    public function pointslogOp() {
		$condition_arr = $list_log = array();
        $condition_arr['pl_memberid'] = $this->member_info['member_id'];
  
        //分页
        $page   = new Page();
        $points_model = Model('points');
        $list_log = $points_model->getPointsLogList($condition_arr,$page,'*','');
		if(!empty($list_log)){
			foreach($list_log as $key=>$value){	
				$list_log[$key]['stagetext'] = $this->insertarr($value['pl_stage']);
				$list_log[$key]['addtimetext'] = date('Y-m-d H:i:s',$value['pl_addtime']);
			}
		}

		$page_count = $points_model->gettotalpage();		
		output_data(array('log_list' => $list_log),mobile_page($page_count));
        
    }
	
	private function insertarr($stage) { 
		switch ($stage){
			case 'regist':
				$insertarr = '注册会员';
				break;
			case 'login':
				 $insertarr = '会员登录';
				break;
			case 'comments':
				$insertarr = '评论商品';
				break;
			case 'order':
				$insertarr = '购物消费';
				break;
			case 'pointorder':
				$insertarr = '兑换礼品';
				break;            
			case 'signin':
				$insertarr = '会员签到';
				break;
		}
		return $insertarr;
	}
	/**
     * 检验是否能签到
     */
    public function checksigninOp() {   
		$condition =array();
		$condition['pl_memberid'] = $this->member_info['member_id'];
		$condition['pl_stage'] = 'signin';
		$todate = date('Ymd000000');
		$totime = strtotime($todate);
		$condition['pl_addtime'][] = array('egt',$totime);
		$condition['pl_addtime'][] = array('elt',$totime+86400);
		$points_model = Model('points');
		$log_array = $points_model->getPointsInfo($condition);
		if (!empty($log_array)){
			output_error('已签到');
		}else{	
			$points_signin = intval(C('points_signin'));
			output_data(array('points_signin'=>$points_signin));  
		}
    }
	
	/**
     * 签到 array('pl_memberid'=>'会员编号','pl_membername'=>'会员名称','pl_adminid'=>'管理员编号','pl_adminname'=>'管理员名称','pl_points'=>'积分','pl_desc'=>'描述','orderprice'=>'订单金额','order_sn'=>'订单编号','order_id'=>'订单序号','point_ordersn'=>'积分兑换订单编号');
     */
    public function signin_addOp() {
		$points_signin = intval(C('points_signin'));//签到对得积分数
		$points_model = Model('points');
		$insertarr['pl_memberid'] = $this->member_info['member_id'];
		$insertarr['pl_membername'] = $this->member_info['member_name'];
		$insertarr['pl_points'] = $points_signin;
		//$insertarr['pl_points'] = $points_signin;
		$return = $points_model->savePointsLog('signin',$insertarr,false);
        //var_dump($return);die;
		output_data(array('point'=>$return));        
    }
	/**

     * 红白积分转换信息详细

     */

	public function memberpointsOp(){

        $model_member   = Model('member');

		$red_points=200;//$_POST['red_points'];
		$member_points = $model_member->getMemberInfo(array('member_id'=>$this->member_info['member_id']),'member_red_points,available_predeposit',true);
		//查询红积分数量
		$returnpoints = Model('returnpoint');	
		$order=' order by addtime desc';
		var_dump(33);
	    $repoint=$returnpoints->order($order)->select();
		
		$repoints=$repoint['back_point'];
		$now_red_points=$member_points['member_red_points'];
		$shengyu_points=$now_red_points-$red_points;
		$pl_addtime=strtotime(date('Y-m-d H:i:s'));
		var_dump(111);
		if($now_red_points >= $red_points){//红积分提现条件:积分大于10000且提现比例为100的倍数
			if($red_points % 100 == 0){
				$red_pointcash=($red_points*$repoints)/100;//红积分提现比例
				$member_array   = array();
				$member_array['available_predeposit']    = $member_points['available_predeposit'] + $red_points/100;////增加余额
				$member_array['member_red_points'] = $member_points['member_red_points']-$red_points;
				$update = $model_member->editMember(array('member_id'=>$this->member_info['member_id'],$member_array);
				//减去红积分

				//Model()->execute('update shopwwi_member set member_red_points = member_red_points - '.$red_points.' where member_id='.$this->member_info['member_id']);
				//$update = $model_member->editMember(array('member_id'=>$this->member_info['member_id'],$member_array);
				$memberid=$this->member_info['member_id'];
				$membername=$this->member_info['member_name'];
				//增加余额
				$add_yue=$red_points/100;

				//Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$add_yue.' where member_id='.$_SESSION['member_id']);
				//余额增加日志
				$sql_bc = "insert into shopwwi_pd_log (lg_member_id,lg_member_name,lg_type,lg_av_amount,lg_add_time,lg_desc) values($memberid,'$membername','redtocash',". $add_yue .",$pl_addtime,'分红豆变现转入余额-余额增加')";
				//mysql_query->($sql_bc);
				//积分操作日志
				$back_points=-$red_points;
				$sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values($memberid,'$membername',$back_points,$pl_addtime,'分红豆变现转入余额-分红豆减少','redpoints')";
				//mysql_query->($sql_bc);

			}

		}
		echo "111";

	}

	
		

}
